package com.depth.viewer.manager
{
	import com.depth.viewer.events.AppEvent;
	
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;

	public class ViewerRSLManager extends EventDispatcher implements IViewerRSLManager
	{
		
		private static var instance:IViewerRSLManager;
		
		private var _rslUrls:Array = [];
		
		public function ViewerRSLManager()
		{
		}
		
		public function loadRSLSwf(url:String):void
		{
			var loader:Loader = new Loader();
			var context:LoaderContext = new LoaderContext();
			context.applicationDomain = ApplicationDomain.currentDomain;//必须有这一句，RSL必须和主程序在同一个域中
			loader.load(new URLRequest(url),context);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,localLoadCompleteHandler);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,localLoadIoErrorHandler);
		}
		private function localLoadCompleteHandler(e:Event):void{
			if(_rslUrls.length == 0){
				dispatchEvent(new AppEvent(AppEvent.VIEWR_RSL_LOAD_COMPLETE));
			}else{
				loadNextRSLSwf();
			}
		}
		private function localLoadIoErrorHandler(e:IOErrorEvent):void{
			
		}
		public function loadRSLSwfs(urls:Array):void
		{
			_rslUrls = urls;
			
			loadNextRSLSwf();
		}
		
		private function loadNextRSLSwf():void{
			
		}
		
		public static function  getInstance():IViewerRSLManager
		{
			if(instance == null){
				instance = new ViewerRSLManager();
			}
			return instance;
		}
		public static function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void{
			getInstance().addEventListener(type,listener,useCapture,priority,useCapture);
		}
		public static function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void{
			getInstance().removeEventListener(type,listener,useCapture);
		}
	}
}